﻿using NoteHelper.IBusiness;
using NoteHelper.Utils;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using Unity;

namespace NoteHelper.Account
{
    public partial class Acfl002 : WeifenLuo.WinFormsUI.Docking.DockContent
	{
		private const string Pre = "Acfl_";
		private log4net.ILog logger;
		private DevAge.ComponentModel.IBoundList bd;
        private IAccountFlowManager accountFlowManager;
        public Acfl002()
		{
			InitializeComponent();
		}

		private void Acfl002_Load(object sender, EventArgs e)
		{
			logger = log4net.LogManager.GetLogger("NoteHelper.Account.Acfl002");
            var container = UnityHelper.GetContainer();
            accountFlowManager = container.Resolve<IAccountFlowManager>();
            var accountTable = accountFlowManager.GetAccountList();
            Acfl_AccountId.DataSource = accountTable;
            Acfl_AccountId.DisplayMember = "Name";
            Acfl_AccountId.ValueMember = "Id";
            var flowTypes = accountFlowManager.GetBizTypeList();
            Acfl_BizType.DataSource = flowTypes;
            Acfl_BizType.DisplayMember = "text";
            Acfl_BizType.ValueMember = "id";

            displayGrid.FixedRows = 1;
			displayGrid.FixedColumns = 1;
            CreateColumns(accountTable, flowTypes);
            
            var ht = new Hashtable();
            ht.Add("FromDate", DateTime.Today.AddDays(-3));
            ht.Add("ToDate", DateTime.Today);
            RowData = ht;
            Query();
        }
        private void CreateColumns(DataTable accountTable, DataTable flowTypes)
        {
            DevAge.Drawing.RectangleBorder border = new DevAge.Drawing.RectangleBorder(new DevAge.Drawing.BorderLine(Color.ForestGreen), new DevAge.Drawing.BorderLine(Color.ForestGreen));
            SourceGrid.Cells.Views.Cell viewString = new SourceGrid.Cells.Views.Cell();
            viewString.Border = border;
            SourceGrid.DataGridColumn gridColumn;
            gridColumn = displayGrid.Columns.Add("FlowSeq", "流水", typeof(string));
            gridColumn.DataCell.View = viewString;
            gridColumn.Width = 50;

            gridColumn = AccountFormatter(accountTable);

            string dtFormat2 = "yyyy-MM-dd";
            string[] dtParseFormats = new string[] { dtFormat2 };
            System.Globalization.DateTimeStyles dtStyles = System.Globalization.DateTimeStyles.AllowInnerWhite | System.Globalization.DateTimeStyles.AllowLeadingWhite | System.Globalization.DateTimeStyles.AllowTrailingWhite | System.Globalization.DateTimeStyles.AllowWhiteSpaces;
            TypeConverter dtConverter = new DevAge.ComponentModel.Converter.DateTimeTypeConverter(dtFormat2, dtParseFormats, dtStyles);
            SourceGrid.Cells.Editors.TextBoxUITypeEditor editorDt2 = new SourceGrid.Cells.Editors.TextBoxUITypeEditor(typeof(DateTime));
            editorDt2.TypeConverter = dtConverter;
            gridColumn = displayGrid.Columns.Add("HappenDate", "发生日期", editorDt2);
            gridColumn.DataCell.View = viewString;
            gridColumn.Width = 80;
            gridColumn = displayGrid.Columns.Add("Description", "备注", typeof(string));
            gridColumn.DataCell.View = viewString;
            gridColumn.Width = 200;

            gridColumn = FlowTypeFormatter(flowTypes);
            gridColumn.DataCell.View = viewString;

            SourceGrid.Cells.Views.Cell viewRight = new SourceGrid.Cells.Views.Cell();
            viewRight.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleRight;
            viewRight.Border = border;

            SourceGrid.Cells.Editors.TextBox currencyEditor = new SourceGrid.Cells.Editors.TextBox(typeof(decimal));
            currencyEditor.TypeConverter = new DevAge.ComponentModel.Converter.CurrencyTypeConverter(typeof(decimal));
            gridColumn = displayGrid.Columns.Add("Amount", "发生金额", currencyEditor);
            gridColumn.DataCell.View = viewRight;
            gridColumn.Width = 100;
            gridColumn = displayGrid.Columns.Add("LeftAmount", "期末余额", currencyEditor);
            gridColumn.DataCell.View = viewRight;
            gridColumn.Width = 100;

            // 创建显示的样式
            SourceGrid.Cells.Views.Cell flowOutView = new SourceGrid.Cells.Views.Cell();
            flowOutView.BackColor = Color.DarkSeaGreen;
            flowOutView.Border = border;

            SourceGrid.Cells.Views.Cell viewNumeric = new SourceGrid.Cells.Views.Cell();
            viewNumeric.BackColor = Color.DarkSeaGreen;
            viewNumeric.Border = border;
            viewNumeric.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleRight;

            // 创建条件判断
            SourceGrid.Conditions.ConditionView condition1 = new SourceGrid.Conditions.ConditionView(flowOutView);
            condition1.EvaluateFunction = delegate (SourceGrid.DataGridColumn column, int gridRow, object itemRow)
            {
                DataRowView row = (DataRowView)itemRow;
                return row["BizType"].ToString() == "-1";
            };
            SourceGrid.Conditions.ConditionView condition2 = new SourceGrid.Conditions.ConditionView(viewNumeric);
            condition2.EvaluateFunction = delegate (SourceGrid.DataGridColumn column, int gridRow, object itemRow)
            {
                DataRowView row = (DataRowView)itemRow;
                return row["BizType"].ToString() == "-1";
            };

            SourceGrid.Cells.Views.ColumnHeader viewColumnHeader = new SourceGrid.Cells.Views.ColumnHeader();
            viewColumnHeader.Font = new Font("宋体", 10, FontStyle.Bold);
            viewColumnHeader.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            viewColumnHeader.Border = border;
            foreach (SourceGrid.DataGridColumn col in displayGrid.Columns)
            {
                col.HeaderCell.View = viewColumnHeader;
                if (col.PropertyName == "Amount" || col.PropertyName == "LeftAmount") {
                    col.Conditions.Add(condition2);
                }
                else
                {
                    col.Conditions.Add(condition1);
                }
            }
        }

        private SourceGrid.DataGridColumn AccountFormatter(DataTable table)
        {
            int count = table.Rows.Count;
            string[] standardValues = new string[count];
            string[] displayNames = new string[count];
            for (int i = 0; i < count; i++)
            {
                DataRow row = table.Rows[i];
                standardValues[i] = row["Id"].ToString();
                displayNames[i] = row["Name"].ToString();
            }
            var editor = new SourceGrid.Cells.Editors.ComboBox(typeof(string));
            editor.StandardValues = standardValues;
            editor.StandardValuesExclusive = true;
            editor.Control.FormattingEnabled = true;

            var mapping = new DevAge.ComponentModel.Validator.ValueMapping();
            mapping.ValueList = standardValues;
            mapping.DisplayStringList = displayNames;
            mapping.BindValidator(editor);
            var gridColumn = displayGrid.Columns.Add("AccountId", "主账号", editor);
            gridColumn.Width = 100;
            return gridColumn;
        }

        private SourceGrid.DataGridColumn FlowTypeFormatter(DataTable table)
        {
            int count = table.Rows.Count;
            int[] standardValues = new int[count];
            string[] displayNames = new string[count];
            for (int i = 0; i < count; i++)
            {
                DataRow row = table.Rows[i];
                standardValues[i] = (int)row["id"];
                displayNames[i] = row["text"].ToString();
            }
            var editor = new SourceGrid.Cells.Editors.ComboBox(typeof(int));
            editor.StandardValues = standardValues;
            editor.StandardValuesExclusive = true;
            editor.Control.FormattingEnabled = true;

            var mapping = new DevAge.ComponentModel.Validator.ValueMapping();
            mapping.ValueList = standardValues;
            mapping.DisplayStringList = displayNames;
            mapping.BindValidator(editor);
            var gridColumn = displayGrid.Columns.Add("BizType", "业务类型", editor);
            gridColumn.Width = 80;
            return gridColumn;
        }
        private DataTable _displayList;
        public DataTable DisplayList
        {
            get { return _displayList; }
            set
            {
                if (value == null) return;
                _displayList = value;

                bd = new DevAge.ComponentModel.BoundDataView(_displayList.DefaultView);
                bd.AllowNew = false;
                bd.AllowEdit = false;
                bd.AllowDelete = false;
                displayGrid.DataSource = bd;
            }
        }
        public Hashtable RowData
        {
            get
            {
                return splitContainer1.Panel1.GetData(Pre);
            }
            set
            {
                splitContainer1.Panel1.SetData(Pre, value);
            }
        }
        private void Query()
        {
            var table = accountFlowManager.QueryAccountFlow(RowData);
            DisplayList = table;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Query();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            RowData = null;
        }
    }
}
